<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: liugohua@clcw.com.cn  Blog:http://blog.sina.com.cn/saverneo    |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\CarDealer;

use AuctionCore\AntNest\UserCenter;
/**
 * 车商列表
 *
 * @author       Eric Lee
 */
class DealerList
{

    /**
     * 获取车商列表
     *
     * @param integer $page
     * @param integer $size
     * @param array   $paramer
     *
     * @return multitype:array(list,total)
     */
    public function getDealers($page = 1, $size = 10, $paramer)
    {
        $map = array();
        $map['cd.level'] = array('gt', 0);
        $where = array();
        //类型
        if ($paramer['keyword']) {
            if(validatePhone($paramer['keyword'])){
                $uc = new UserCenter();
                $info1 = $uc->info_bymobile($paramer['keyword'],2,1);
                $where['du.uid'] = array('eq',$info1['account_id']);
            }else{
                $where['cd.fullname'] = array('like', '%' . $paramer['keyword'] . '%');
                $where['cd.contact_person'] = array('like', '%' . $paramer['keyword'] . '%');
                $where['_logic'] = 'OR';
            }
            $map['_complex'] = $where;
        }
        //所属公司
        if (!empty($paramer['depart_id'])) {
            $map['cd.depart_id'] = array('eq', $paramer['depart_id']);
        }

        $co = M('car_dealer');
        $cols = "cd.dealer_id,cd.contact_person,cd.fullname,cd.depart_id,dep.name as branch_company,du.uid";
        $prefix = C('DB_PREFIX');
        $total = $co->alias('cd')
                ->join($prefix . "dealer_user as du ON cd.dealer_id=du.dealer_id")
                ->where($map)->count();
        $list = $co->alias('cd')->field($cols)
            ->join($prefix . "dealer_user as du ON cd.dealer_id=du.dealer_id")
            ->join("LEFT JOIN " . $prefix . "depart as dep ON cd.depart_id=dep.id")
            ->where($map)->limit($size)->page($page)->order("dealer_id desc")->select();
        if(!empty($list)){
            $dealer = new Dealer();
            $list= $dealer->get_contact_mobile_byAntNest($list);
        }
        foreach ($list as $key => $val) {
            $list[$key] = $val;
            if ($val['depart_id'] > 0) {
                $d = \AuctionCore\Account\Back\Department::getDepartById($val['depart_id']);
            }
            $list[$key]['branch_company'] = $d['name'];
        }
        //echo M()->getLastSql();
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 获取车商代金券列表
     *
     * @param integer $page
     * @param integer $size
     * @param array   $paramer
     *
     * @return multitype:array(list,total)
     */
    public function getDealerCoupons($page = 1, $size = 10, $paramer)
    {
        $map = array();
        //类型
        $map['dc.dealer_id'] = $paramer['dealer_id'];
        switch ($paramer['curr_tab']) {
            case '0'://未使用
                $where = array();
                $where['dc.use_status'] = array('eq', '0');
                $where['c.end_date'] = array('egt', date('Y-m-d'));
                $map['_complex'] = $where;
                break;
            case '1'://已使用
                $map['dc.use_status'] = array('eq', '1');
                break;
            case '2'://已过期
                $map['dc.use_status'] = array('neq', '1');
                $map['c.end_date'] = array('lt', date('Y-m-d'));
                break;
            default:
                $where = array();
                $where['c.use_status'] = array('eq', '0');
                $where['c.end_date'] = array('egt', date('Y-m-d'));
                $map['_complex'] = $where;
                break;
        }
        if ($paramer['coupon_type_id']) {
            $map['c.coupon_type_id'] = array('eq', $paramer['coupon_type_id']);
        }
        if ($paramer['get_time']) {
            $map['dc.createtime'] = array('like', date('Y-m-d', strtotime($paramer['get_time'])) . '%');
        }
        if ($paramer['end_date']) {
            $map['c.end_date'] = array('like', date('Y-m-d', strtotime($paramer['end_date'])) . '%');
        }
        $prefix = C('DB_PREFIX');
        $co = M('dealer_coupon');
        $total = $co->alias('dc')
            ->join($prefix . "coupon c ON c.coupon_id=dc.coupon_id")
            ->join($prefix . "coupon_type ct on ct.coupon_type_id=c.coupon_type_id")
            ->where($map)->count();

        $cols = 'c.coupon_no,c.amount,ct.type_name,dc.createtime,c.start_date,c.end_date';
        $list = $co->alias('dc')->field($cols)
            ->join($prefix . "coupon c ON c.coupon_id=dc.coupon_id")
            ->join(C('DB_PREFIX') . "coupon_type ct on ct.coupon_type_id=c.coupon_type_id")
            ->where($map)->limit($size)->page($page)->order("dc.dc_id")->select();
        //echo $co->getLastSql();
        foreach ($list as $key => $val) {
        }
        //echo M()->getLastSql();
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     *车商统计列表
     *
     */
    public function get_dealer_statistics_List($page = 1, $size = 10, $paramer)
    {
        $in_map = array();
        $out_map = array();
        if (!empty($paramer['account_manage'])) {
            if ($paramer['account_manage'] > 0) {
                $out_map['dm.emp_id'] = ['eq', $paramer['account_manage']];
            }
            if ($paramer['account_manage'] == '-1') {
                $where1['dm.emp_id'] = ['lt',1];
                $out_map['dm.emp_id'] = ['exp','is NULL'];
                $out_map['_complex'] = $where1;
                $out_map['_logic'] = "OR";
            }
        }
        if ($paramer['keyword']) {
            $mobile_rules = "/^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|17[0-9]{9}|18[0-9]{9}$/";
            if(preg_match($mobile_rules,trim($paramer['keyword']))){
                $uc = new UserCenter();
                $result = $uc->info_bymobile(trim($paramer['keyword']), 2);
                if (!empty($result))
                    $where['du.uid'] = $result['account_id'];
            }else {
                $where['cd.dealer_id'] = array('eq', $paramer['keyword']);
                $where['cd.contact_person'] = array('eq', trim($paramer['keyword']));
                //$where['cd.contact_mobile'] = array('eq', trim($paramer['keyword']));
                $where['_logic'] = 'OR';
            }
            $out_map['_complex'] = $where;
        }
        if ($paramer['account_type'] > -1) {
            if ($paramer['account_type'] > 0) {
                $out_map['cd.auth_type'] = array('eq', $paramer['account_type']);
                $out_map['cd.auth_status'] = array('eq', 2);
            } else {
                $out_map['cd.auth_status'] = array('neq', 2);
            }
        }
        if (!empty($paramer['start_time']) || !empty($paramer['end_time'])) {
            if (empty($paramer['start_time'])) {
                $out_map['cd.createtime'] = array("elt", $paramer['end_time'] . " 23:59:59");
            } elseif (empty($paramer['end_time'])) {
                $out_map['cd.createtime'] = array("egt", $paramer['start_time'] . " 00:00:00");
            } else {
                $out_map['cd.createtime'] = array(array("egt", $paramer['start_time'] . " 00:00:00"), array("elt", $paramer['end_time'] . " 23:59:59"));
            }
        }
        if (!empty($paramer['dealer_start_time']) || !empty($paramer['dealer_end_time'])) {
            if (empty($paramer['dealer_start_time'])) {
                $in_map['ds.date'] = array("elt", $paramer['dealer_end_time']);
            } elseif (empty($paramer['dealer_end_time'])) {
                $in_map['ds.date'] = array("egt", $paramer['dealer_start_time']);
            } else {
                $in_map['ds.date'] = array(array("egt", $paramer['dealer_start_time']), array("elt", $paramer['dealer_end_time']));
            }
        }
        if ($paramer['branch_id']) {
            $out_map['cd.depart_id'] = array('eq', $paramer['branch_id']);
        }
        if ($paramer['emp_id'] && empty($paramer['account_manage'])) {
            $out_map['e.emp_id'] = array('eq', $paramer['emp_id']);
        }
//        $out_map['u.status'] = array('eq',0);
        // $order = "u.reg_date desc"; 
        switch ($paramer['order_mode']) {
            case '1':
                $order = "a.deal_num desc";
                break;
            case '2':
                $order = "a.deal_num asc";
                break;
            case '3':
                $order = "a.login_num desc ";
                break;
            case '4':
                $order = "a.login_num asc ";
                break;
            case '5':
                $order = "a.follow_num desc";
                break;
            case '6':
                $order = "a.follow_num asc";
                break;
            case '7':
                $order = "a.bid_num desc";
                break;
            case '8':
                $order = "a.bid_num asc";
                break;
            case '9':
                $order = "a.Auction_num desc";
                break;
            case '10':
                $order = "a.Auction_num asc";
                break;
            case '11':
                $order = "a.Auction_num asc";
                break;
            case '12':
                $order = "a.Auction_num asc";
                break;
            default:
                $order = "cd.createtime desc";
                break;
        }
        $prefix = C('DB_PREFIX');
        $cols = 'sum(ds.follow_num) as follow_num,sum(ds.bid_num) as bid_num,sum(ds.Auction_num) as auction_num,
                 sum(ds.deal_num) as deal_num,sum(ds.login_num) as login_num,sum(ds.minus_num) as minus_num,
                 ds.dealer_id';
        $subQuery = M('dealer_statistics')->alias('ds')->field($cols)
            ->where($in_map)->group('ds.dealer_id')->buildSql();

        $cols1 = "cd.level,cd.bail_amount,cd.freeze_amount,cd.indemnity_amount,cd.minus_amount,cd.dealer_id,
                  cd.contact_person,du.uid,cd.auth_type,cd.auth_status,
                  e.real_name,a.follow_num,
                  a.bid_num,a.Auction_num,a.deal_num,a.login_num,a.minus_num";
        $total = M('car_dealer')->alias('cd')
            ->join($prefix . "dealer_user as du ON cd.dealer_id=du.dealer_id")
            ->join(' left join ' . $subQuery . " a on cd.dealer_id = a.dealer_id")
            ->join(' left join ' . $prefix . "dealer_manager dm on dm.dealer_id = cd.dealer_id ")
            ->join(' left join ' . $prefix . "employee e on e.emp_id = dm.emp_id")
            ->where($out_map)->count();

        $list = M('car_dealer')->alias('cd')->field($cols1)
            ->join($prefix . "dealer_user as du ON cd.dealer_id=du.dealer_id")
            ->join(' left join ' . $subQuery . " a on cd.dealer_id = a.dealer_id")
            ->join(' left join ' . $prefix . "dealer_manager dm on dm.dealer_id = cd.dealer_id ")
            ->join(' left join ' . $prefix . "employee e on e.emp_id = dm.emp_id")
            ->where($out_map)->order($order)->limit($size)->page($page)->select();
        if(!empty($list)){
            $dealer = new Dealer();
            $list= $dealer->get_contact_mobile_byAntNest($list);
        }
        foreach ($list as $key => $value) {
            $list[$key]['total_amount'] = $value['bail_amount'] + $value['freeze_amount'] + $value['indemnity_amount'];
            $list[$key]['bail_amount'] = $value['bail_amount'] + 0;
            $list[$key]['freeze_amount'] = $value['freeze_amount'] + 0;
            $list[$key]['indemnity_amount'] = $value['indemnity_amount'] + 0;
            $list[$key]['follow_num'] = $value['follow_num'] + 0;
            $list[$key]['bid_num'] = $value['bid_num'] + 0;
            $list[$key]['auction_num'] = $value['auction_num'] + 0;
            $list[$key]['deal_num'] = $value['deal_num'] + 0;
            $list[$key]['login_num'] = $value['login_num'] + 0;
            $list[$key]['minus_num'] = $value['minus_num'] + 0;
            $list[$key]['real_name'] = $value['real_name'] ? $value['real_name'] : '未分配';
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }


}
//end
